/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 菜单项
    /// </summary>
    public class FCMenuItem : FCButton {
        /// <summary>
        /// 创建菜单项
        /// </summary>
        public FCMenuItem() {
            setFont(new FCFont("Default", 12, false, false, false));
            m_size = new FCSize(200, 25);
        }

        /// <summary>
        /// 创建菜单项
        /// </summary>
        /// <param name="text">文字</param>
        public FCMenuItem(String text) {
            setFont(new FCFont("Default", 12, false, false, false));
            m_size = new FCSize(200, 25);
            setText(text);
        }

        /// <summary>
        /// 所有菜单项
        /// </summary>
        public ArrayList<FCMenuItem> m_items = new ArrayList<FCMenuItem>();

        public bool m_checked = false;

        /// <summary>
        /// 获取是否选中
        /// </summary>
        public virtual bool isChecked()
        {
            return m_checked;
        }

        /// <summary>
        /// 设置是否选中
        /// </summary>
        public virtual void setChecked(bool value)
        {
            m_checked = value;
        }

        public FCMenu m_dropDownMenu;

        /// <summary>
        /// 获取下拉表格
        /// </summary>
        public virtual FCMenu getDropDownMenu()
        {
            return m_dropDownMenu;
        }

        /// <summary>
        /// 设置下拉表格
        /// </summary>
        public virtual void setDropDownMenu(FCMenu value)
        {
            m_dropDownMenu = value;
        }

        public FCMenuItem m_parentItem;

        /// <summary>
        /// 获取父菜单项
        /// </summary>
        public virtual FCMenuItem getParentItem()
        {
            return m_parentItem;
        }

        /// <summary>
        /// 设置父菜单项
        /// </summary>
        public virtual void setParentItem(FCMenuItem value)
        {
            m_parentItem = value;
        }

        public FCMenu m_parentMenu;

        /// <summary>
        /// 获取置所在菜单
        /// </summary>
        public virtual FCMenu getParentMenu()
        {
            return m_parentMenu;
        }

        /// <summary>
        /// 设置所在菜单
        /// </summary>
        public virtual void setParentMenu(FCMenu value)
        {
            m_parentMenu = value;
        }

        public String m_value;

        /// <summary>
        /// 获取值
        /// </summary>
        public virtual String getValue()
        {
            return m_value;
        }

        /// <summary>
        /// 设置值
        /// </summary>
        public virtual void setValue(String value)
        {
            m_value = value;
        }

        /// <summary>
        /// 添加子菜单项
        /// </summary>
        /// <param name="item">菜单项</param>
        public virtual void addItem(FCMenuItem item) {
            item.setParentItem(this);
            item.setParentMenu(getParentMenu());
            item.onAddingItem(-1);
            m_items.add(item);
        }

        /// <summary>
        /// 清除所有的项
        /// </summary>
        public virtual void clearItems() {
            ArrayList<FCMenuItem> itemsCopy = new ArrayList<FCMenuItem>();
            int itemSize = m_items.size();
            for (int i = 0; i < itemSize; i++) {
                itemsCopy.add(m_items.get(i));
            }
            int copySize = itemsCopy.size();
            for (int i = 0; i < copySize; i++) {
                itemsCopy.get(i).onRemovingItem();
                itemsCopy.get(i).delete();
            }
            m_items.clear();
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "MenuItem";
        }

        /// <summary>
        /// 获取所有的项
        /// </summary>
        /// <returns></returns>
        public virtual ArrayList<FCMenuItem> getItems() {
            return m_items;
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public override void getAttribute(String name, ref String value, ref String type) {
            if (name == "checked") {
                type = "bool";
                value = FCTran.boolToStr(isChecked());
            }
            else if (name == "value") {
                type = "text";
                value = getValue();
            }
            else {
                base.getAttribute(name, ref value, ref type);
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns>属性名称列表</returns>
        public override ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.AddRange(new String[] { "Checked", "Value" });
            return attributeNames;
        }

        /// <summary>
        /// 插入项
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="item">菜单项</param>
        public void insertItem(int index, FCMenuItem item) {
            item.setParentItem(this);
            item.setParentMenu(getParentMenu());
            item.onAddingItem(index);
            m_items.Insert(index, item);
        }

        /// <summary>
        /// 添加项
        /// </summary>
        /// <param name="index">行索引</param>
        public virtual void onAddingItem(int index) {
            FCMenu dropDownMenu = null;
            if (m_parentItem != null) {
                dropDownMenu = m_parentItem.getDropDownMenu();
                if (dropDownMenu == null) {
                    dropDownMenu = m_parentMenu.createDropDownMenu();
                    dropDownMenu.setVisible(false);
                    m_parentItem.setDropDownMenu(dropDownMenu);
                    dropDownMenu.setParentItem(m_parentItem);
                    m_parentMenu.getNative().addView(dropDownMenu);
                }
            }
            else {
                dropDownMenu = m_parentMenu;
            }
            if (dropDownMenu != null) {
                if (index != -1) {
                    //插入行
                    dropDownMenu.insertView(index, this);
                }
                else {
                    //添加行
                    dropDownMenu.addView(this);
                }
                //添加子节点
                int itemSize = m_items.size();
                for (int i = 0; i < itemSize; i++) {
                    m_items.get(i).onAddingItem(-1);
                }
            }
        }

        /// <summary>
        /// 点击方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onClick(FCTouchInfo touchInfo) {
            base.onClick(touchInfo);
            if (m_parentMenu != null) {
                m_parentMenu.onMenuItemClick(this, touchInfo);
            }
        }

        /// <summary>
        /// 触摸移动方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchMove(FCTouchInfo touchInfo) {
            base.onTouchMove(touchInfo);
            if (m_parentMenu != null) {
                m_parentMenu.onMenuItemTouchMove(this, touchInfo);
            }
        }

        /// <summary>
        /// 重绘前景方法 
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪矩形</param>
        public override void onPaintForeground(FCPaint paint, FCRect clipRect) {
            int width = getWidth(), height = getHeight();
            if (width > 0 && height > 0) {
                int right = width;
                int midY = height / 2;
                String text = getText();
                int tRight = 0;
                long textColor = getPaintingTextColor();
                if (text != null && text.Length > 0) {
                    FCFont font = getFont();
                    FCSize tSize = paint.textSize(text, font, -1);
                    FCRect tRect = new FCRect();
                    tRect.left = 10;
                    tRect.top = midY - tSize.cy / 2;
                    tRect.right = tRect.left + tSize.cx;
                    tRect.bottom = tRect.top + tSize.cy;
                    paint.drawText(text, textColor, font, tRect, -1);
                    tRight = tRect.right + 4;
                }
                //绘制选中
                if (m_checked) {
                    FCRect eRect = new FCRect(tRight, height / 2 - 4, tRight + 8, height / 2 + 4);
                    paint.fillEllipse(textColor, eRect);
                }
                //画子菜单的提示箭头
                if (m_items.size() > 0) {
                    FCPoint point1 = new FCPoint(), point2 = new FCPoint(), point3 = new FCPoint();
                    FCMenu menu = m_parentMenu;
                    if (m_parentItem != null) {
                        menu = m_parentItem.getDropDownMenu();
                    }
                    FCLayoutStyle layoutStyle = menu.getLayoutStyle();
                    //横向
                    if (layoutStyle == FCLayoutStyle.LeftToRight || layoutStyle == FCLayoutStyle.RightToLeft) {
                        point1.x = right - 25;
                        point1.y = midY - 2;
                        point2.x = right - 14;
                        point2.y = midY - 2;
                        point3.x = right - 20;
                        point3.y = midY + 4;
                    }
                    //纵向
                    else {
                        point1.x = right - 15;
                        point1.y = midY;
                        point2.x = right - 25;
                        point2.y = midY - 5;
                        point3.x = right - 25;
                        point3.y = midY + 5;
                    }
                    FCPoint[] points = new FCPoint[] { point1, point2, point3 };
                    paint.fillPolygon(textColor, points);
                }
            }
        }

        /// <summary>
        /// 移除项方法
        /// </summary>
        public virtual void onRemovingItem() {
            FCMenu dropDownMenu = null;
            if (m_parentItem != null) {
                dropDownMenu = m_parentItem.getDropDownMenu();
            }
            else {
                dropDownMenu = m_parentMenu;
            }
            if (dropDownMenu != null) {
                if (m_items != null && m_items.size() > 0) {
                    int itemSize = m_items.size();
                    for (int i = 0; i < itemSize; i++) {
                        m_items.get(i).onRemovingItem();
                    }
                }
                dropDownMenu.removeView(this);
            }
            if (m_dropDownMenu != null) {
                m_parentMenu.getNative().removeView(m_dropDownMenu);
                m_dropDownMenu.delete();
                m_dropDownMenu = null;
            }
        }

        /// <summary>
        /// 移除子菜单项
        /// </summary>
        /// <param name="item">子菜单项</param>
        public void removeItem(FCMenuItem item) {
            item.onRemovingItem();
            m_items.remove(item);
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value) {
            if (name == "checked") {
                setChecked(FCTran.strToBool(value));
            }
            else if (name == "value") {
                setValue(value);
            }
            else {
                base.setAttribute(name, value);
            }
        }
    }
}
